package com.example.dwhne.leonweather;

import android.content.Intent;
import android.content.SharedPreferences;
import android.content.pm.ApplicationInfo;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerTabStrip;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

import interfaces.heweather.com.interfacesmodule.bean.Lang;
import interfaces.heweather.com.interfacesmodule.bean.Unit;
import interfaces.heweather.com.interfacesmodule.bean.air.now.AirNow;
import interfaces.heweather.com.interfacesmodule.bean.air.now.AirNowCity;
import interfaces.heweather.com.interfacesmodule.bean.basic.Basic;
import interfaces.heweather.com.interfacesmodule.bean.weather.forecast.Forecast;
import interfaces.heweather.com.interfacesmodule.bean.weather.forecast.ForecastBase;
import interfaces.heweather.com.interfacesmodule.bean.weather.now.Now;
import interfaces.heweather.com.interfacesmodule.bean.weather.now.NowBase;
import interfaces.heweather.com.interfacesmodule.view.HeWeather;

public class WeatherShow extends AppCompatActivity {
    private VpSwipeRefreshLayout weather_flash;
    private int cityNum=0;
    private int nowPosition;
    private int lastPosition;
    private String cityCNs[]=new String[5];
    private String cityParents[]=new String[5];
    private ViewPager pagers;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_weather_show);
        //初始化全屏显示
        transparentStatusBar();
        //初始化数据获取
        dataGetIni();
        //初始化viewPager指示器
        indicatorIni();
        //通过ViewPager结合Fragment实现翻页及显示
        fragmentIni();
        //手动下拉刷新设置
        refreshIni();
        //自动刷新设置
        timerIni();
        //获取当前界面城市的天气信息
        WeatherGet(cityCNs[nowPosition]);
        //翻页切换城市
        pagers.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int i, float v, int i1) {
            }
            @Override
            public void onPageSelected(int i) {
                nowPosition=i;
                indicatorLinearLayout.getChildAt(lastPosition).setEnabled(false);
                indicatorLinearLayout.getChildAt(nowPosition).setEnabled(true);
                lastPosition = nowPosition;
                if (weatherList.get(nowPosition)==null){
                    WeatherGet(cityCNs[i]);

                }else{
                    WeatherShow(nowPosition);
                }
            }
            @Override
            public void onPageScrollStateChanged(int i) {

            }
        });
        //点击按钮进入城市管理界面
        ImageButton button1=findViewById(R.id.weatherShow_Button1);
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
//                Toast.makeText(WeatherShow.this, "按钮被点击了", Toast.LENGTH_SHORT).show();
                Intent intent=new Intent(WeatherShow.this,CityManage.class);//构建一个intent对象，第一个参数：上下文，第二个：目标活动
                startActivity(intent);
            }
        });
    }
    @Override
    protected void onNewIntent(Intent intent) {

        //初始数据获取
//        dataGetIni();
        //通过ViewPager结合Fragment实现翻页及显示
        fragmentIni();
        super.onNewIntent(intent);
    }
    @Override
    protected void onDestroy() {
//        stopService(new Intent(WeatherShow.this,weatherFlash.class));
        timer.cancel();
        timer = null;
        timertask.cancel();
        timertask = null;
        super.onDestroy();
    }

    //*天气信息获取
    private void WeatherGet(final String cityCN){
        HeWeather.getWeatherNow(WeatherShow.this, cityCN, Lang.CHINESE_SIMPLIFIED, Unit.METRIC,
                new HeWeather.OnResultWeatherNowBeanListener() {
                    @Override
                    public void onError(Throwable e) {
//                        Log.i("Test", "onError: "+e);
                        Toast toast = Toast.makeText(WeatherShow.this, "请求超时，请检查网络连接并下拉刷新", Toast.LENGTH_SHORT);
                        toast.setGravity(Gravity.TOP, 0, 10);
                        toast.show();
                    }
                    @Override
                    public void onSuccess(List<Now> dataObject) {
                        Now now=dataObject.get(0);
                        Basic basic=now.getBasic();
                        NowBase nowBase=now.getNow();
//                        Update uptime=now.getUpdate();
                        //List lll=new ArrayList();
                        //lll.add(basic.getLocation());
                        Map<String,String> map=new HashMap<String, String>();
                        map.put("Location",basic.getLocation());//城市名称
                        map.put("Tmp",nowBase.getTmp());//温度

//                        ApplicationInfo appInfo =getApplicationInfo();
//                        int resID = getResources().getIdentifier("a"+nowBase.getCond_code(), "weather_Icon", appInfo.packageName);
//                        Resources resources = getResources();
//                        Drawable drawable = resources.getDrawable(resID);
                        map.put("Cond_code",nowBase.getCond_code());//城市天气状况代码
                        map.put("Cond_txt",nowBase.getCond_txt());
                        map.put("Wind_dir",nowBase.getWind_dir());
                        map.put("Wind_sc",nowBase.getWind_sc());
                        weatherList.set(nowPosition,map);
//                        Log.i("Test", "WeatherGet "+lll.get(0));
                        Toast toast = Toast.makeText(WeatherShow.this, basic.getLocation()+"天气更新成功", Toast.LENGTH_SHORT);
                        toast.setGravity(Gravity.TOP, 0, 10);
                        toast.show();
//                        getFlog[0]=true;
//                        Log.i("Test", "实时天气获取成功 ");

                        HeWeather.getWeatherForecast(WeatherShow.this, cityCN, Lang.CHINESE_SIMPLIFIED, Unit.METRIC, new HeWeather.OnResultWeatherForecastBeanListener() {
                            @Override
                            public void onError(Throwable throwable) {

                            }

                            @Override
                            public void onSuccess(List<Forecast> list) {
                                List<ForecastBase> forecast=list.get(0).getDaily_forecast();
                                List<Map<String,Object>> tList=new ArrayList<Map<String,Object>>();
                                String txt1[]={"今天","明天","后天"};
                                for(int i=0;i<3;i++){
//                    Log.i("Test", "onSuccess: "+new Gson().toJson(forecast.get(0)));//今天
                                    Map<String,Object> map=new HashMap<String, Object>();
                                    map.put("Tmp",forecast.get(i).getTmp_max()+" / "+forecast.get(i).getTmp_min()+"℃");//最高气温
                                    map.put("Cond_txt",txt1[i]+"·"+forecast.get(i).getCond_txt_d());//天气信息
                                    int pngID=getResources().getIdentifier("a"+forecast.get(i).getCond_code_d(), "drawable", getApplicationInfo().packageName);
                                    map.put("Icon",pngID);//天气状况图标XXXXX要改
                                    map.put("Air","良");//免费版无法获取空气质量预报
                                    tList.add(i,map);
                                }
                                forcastList.set(nowPosition,tList);
//                                getFlog[1]=true;
                                HeWeather.getAirNow(WeatherShow.this, cityParents[nowPosition], Lang.CHINESE_SIMPLIFIED, Unit.METRIC, new HeWeather.OnResultAirNowBeansListener() {
                                    @Override
                                    public void onError(Throwable throwable) {

                                    }

                                    @Override
                                    public void onSuccess(List<AirNow> list) {
                                        AirNow air=list.get(0);
                                        AirNowCity airNowCity=air.getAir_now_city();
                                        Map<String,String> map=new HashMap<String, String>();
                                        if(airNowCity.getQlty().equals("优")|airNowCity.getQlty().equals("良")){
                                            map.put("Qlty","空气"+airNowCity.getQlty());//空气质量描述
                                        }else{
                                            map.put("Qlty",airNowCity.getQlty());//空气质量描述
                                        }
                                        map.put("Aqi",airNowCity.getAqi());//空气质量指数
                                        airNowList.set(nowPosition,map);
//                                        getFlog[2]=true;
                                        WeatherShow(nowPosition);
                                    }
                                });
                            }
                        });
                    }
                });

    }

    //*天气显示
    private void WeatherShow(int Position){


        switch (Position){
            case 0:
                View include1 =findViewById(R.id.weather1);
                //城市名
                TextView cityNameNow1=include1.findViewById(R.id.weatherShow_cityName);
                cityNameNow1.setText((String)weatherList.get(nowPosition).get("Location"));
                //天气图标
                ImageView weatherNowIcon1=include1.findViewById(R.id.weatherShow_Icon1);
                weatherNowIcon1.setBackground(getIDbyName("a"+weatherList.get(nowPosition).get("Cond_code")));

                //温度
                TextView weatherTemp1=include1.findViewById(R.id.weatherShow_Temp1);
                weatherTemp1.setText((String)weatherList.get(nowPosition).get("Tmp"));
                //实时空气质量
                TextView weatherairQuality1=include1.findViewById(R.id.weatherShow_airQuality1);
                weatherairQuality1.setText(airNowList.get(nowPosition).get("Qlty")+" "+airNowList.get(nowPosition).get("Aqi"));
                //天气描述
                TextView weatherExplain1=include1.findViewById(R.id.weatherShow_Explain);
                weatherExplain1.setText(weatherList.get(nowPosition).get("Cond_txt")+" "+weatherList.get(nowPosition).get("Wind_dir")+weatherList.get(nowPosition).get("Wind_sc")+"级");
                //三天天气
                ListView listView1=include1.findViewById(R.id.weatherShow_ListView);
                SimpleAdapter sim_Adapter1=new SimpleAdapter(this,forcastList.get(nowPosition),R.layout.forecast_item,new String[]{"Cond_txt","Air","Icon","Tmp"},new int[]{R.id.weathetr_TWeather,R.id.weathetr_TAir,R.id.weathetr_TIcon,R.id.weathetr_TTemp});
                listView1.setAdapter(sim_Adapter1);
                break;
            case 1:
                View include2 = (View) findViewById(R.id.weather2);
                //城市名
                TextView cityNameNow2=include2.findViewById(R.id.weatherShow_cityName);
                cityNameNow2.setText((String)weatherList.get(nowPosition).get("Location"));
                //天气图标
                ImageView weatherNowIcon2=include2.findViewById(R.id.weatherShow_Icon1);
                weatherNowIcon2.setBackground(getIDbyName("a"+weatherList.get(nowPosition).get("Cond_code")));
                //温度
                TextView weatherTemp2=include2.findViewById(R.id.weatherShow_Temp1);
                weatherTemp2.setText((String)weatherList.get(nowPosition).get("Tmp"));
                //实时空气质量
                TextView weatherairQuality2=include2.findViewById(R.id.weatherShow_airQuality1);
                weatherairQuality2.setText(airNowList.get(nowPosition).get("Qlty") +" "+airNowList.get(nowPosition).get("Aqi"));
                //天气描述
                TextView weatherExplain2=include2.findViewById(R.id.weatherShow_Explain);
                weatherExplain2.setText(weatherList.get(nowPosition).get("Cond_txt")+" "+weatherList.get(nowPosition).get("Wind_dir")+weatherList.get(nowPosition).get("Wind_sc")+"级");
                //三天天气
                ListView listView2=include2.findViewById(R.id.weatherShow_ListView);
                SimpleAdapter sim_Adapter2=new SimpleAdapter(this,forcastList.get(nowPosition),R.layout.forecast_item,new String[]{"Cond_txt","Air","Icon","Tmp"},new int[]{R.id.weathetr_TWeather,R.id.weathetr_TAir,R.id.weathetr_TIcon,R.id.weathetr_TTemp});
                listView2.setAdapter(sim_Adapter2);
                break;
            case 2:
                View include3 = (View) findViewById(R.id.weather3);
                //城市名
                TextView cityNameNow3=include3.findViewById(R.id.weatherShow_cityName);
                cityNameNow3.setText((String)weatherList.get(nowPosition).get("Location"));
                //天气图标
                ImageView weatherNowIcon3=include3.findViewById(R.id.weatherShow_Icon1);
                weatherNowIcon3.setBackground(getIDbyName("a"+weatherList.get(nowPosition).get("Cond_code")));
                //温度
                TextView weatherTemp3=include3.findViewById(R.id.weatherShow_Temp1);
                weatherTemp3.setText((String)weatherList.get(nowPosition).get("Tmp"));
                //实时空气质量
                TextView weatherairQuality3=include3.findViewById(R.id.weatherShow_airQuality1);
                weatherairQuality3.setText(airNowList.get(nowPosition).get("Qlty") +" "+airNowList.get(nowPosition).get("Aqi"));
                //天气描述
                TextView weatherExplain3=include3.findViewById(R.id.weatherShow_Explain);
                weatherExplain3.setText(weatherList.get(nowPosition).get("Cond_txt")+" "+weatherList.get(nowPosition).get("Wind_dir")+weatherList.get(nowPosition).get("Wind_sc")+"级");
                //三天天气
                ListView listView3=include3.findViewById(R.id.weatherShow_ListView);
                SimpleAdapter sim_Adapter3=new SimpleAdapter(this,forcastList.get(nowPosition),R.layout.forecast_item,new String[]{"Cond_txt","Air","Icon","Tmp"},new int[]{R.id.weathetr_TWeather,R.id.weathetr_TAir,R.id.weathetr_TIcon,R.id.weathetr_TTemp});
                listView3.setAdapter(sim_Adapter3);
                break;
            case 3:
                View include4 = (View) findViewById(R.id.weather4);
                //城市名
                TextView cityNameNow4=include4.findViewById(R.id.weatherShow_cityName);
                cityNameNow4.setText((String)weatherList.get(nowPosition).get("Location"));
//                //天气图标
                ImageView weatherNowIcon4=include4.findViewById(R.id.weatherShow_Icon1);
                weatherNowIcon4.setBackground(getIDbyName("a"+weatherList.get(nowPosition).get("Cond_code")));
//                //温度
                TextView weatherTemp4=include4.findViewById(R.id.weatherShow_Temp1);
                weatherTemp4.setText((String)weatherList.get(nowPosition).get("Tmp"));
                //实时空气质量
                TextView weatherairQuality4=include4.findViewById(R.id.weatherShow_airQuality1);
                weatherairQuality4.setText(airNowList.get(nowPosition).get("Qlty")+" "+airNowList.get(nowPosition).get("Aqi"));
                //天气描述
                TextView weatherExplain4=include4.findViewById(R.id.weatherShow_Explain);
                weatherExplain4.setText(weatherList.get(nowPosition).get("Cond_txt")+" "+weatherList.get(nowPosition).get("Wind_dir")+weatherList.get(nowPosition).get("Wind_sc")+"级");
                //三天天气
                ListView listView4=include4.findViewById(R.id.weatherShow_ListView);
                SimpleAdapter sim_Adapter4=new SimpleAdapter(this,forcastList.get(nowPosition),R.layout.forecast_item,new String[]{"Cond_txt","Air","Icon","Tmp"},new int[]{R.id.weathetr_TWeather,R.id.weathetr_TAir,R.id.weathetr_TIcon,R.id.weathetr_TTemp});
                listView4.setAdapter(sim_Adapter4);
                break;
            case 4:
                View include5 = (View) findViewById(R.id.weather5);
                //城市名
                TextView cityNameNow5=include5.findViewById(R.id.weatherShow_cityName);
                cityNameNow5.setText((String)weatherList.get(nowPosition).get("Location"));
                //天气图标
                ImageView weatherNowIcon5=include5.findViewById(R.id.weatherShow_Icon1);
                weatherNowIcon5.setBackground(getIDbyName("a"+weatherList.get(nowPosition).get("Cond_code")));
                //温度
                TextView weatherTemp5=include5.findViewById(R.id.weatherShow_Temp1);
                weatherTemp5.setText((String)weatherList.get(nowPosition).get("Tmp"));
                //实时空气质量
                TextView weatherairQuality5=include5.findViewById(R.id.weatherShow_airQuality1);
                weatherairQuality5.setText(airNowList.get(nowPosition).get("Qlty")+" "+airNowList.get(nowPosition).get("Aqi"));
                //天气描述
                TextView weatherExplain5=include5.findViewById(R.id.weatherShow_Explain);
                weatherExplain5.setText(weatherList.get(nowPosition).get("Cond_txt")+" "+weatherList.get(nowPosition).get("Wind_dir")+weatherList.get(nowPosition).get("Wind_sc")+"级");
                //三天天气
                ListView listView5=include5.findViewById(R.id.weatherShow_ListView);
                SimpleAdapter sim_Adapter5=new SimpleAdapter(this,forcastList.get(nowPosition),R.layout.forecast_item,new String[]{"Cond_txt","Air","Icon","Tmp"},new int[]{R.id.weathetr_TWeather,R.id.weathetr_TAir,R.id.weathetr_TIcon,R.id.weathetr_TTemp});
                listView5.setAdapter(sim_Adapter5);
                break;
        }
    }


    //* 数据初始化
    private List<Map<String,String>> weatherList;
    private List<Map<String,String>> airNowList;
    private List<List<Map<String,Object>>> forcastList;
    private void dataGetIni(){
        //复杂List初始化
        weatherList = new ArrayList<Map<String,String>>();
        airNowList=new ArrayList<Map<String,String>>();
        forcastList=new ArrayList<List<Map<String,Object>>>();
        for(int i=0;i<5;i++){
            weatherList.add(i,null);
            airNowList.add(i,null);
            List<Map<String,Object>>forcastList0=new ArrayList<Map<String,Object>>();
            for(int j=0;j<3;j++){
                forcastList0.add(j,null);
            }
            forcastList.add(i,forcastList0);
        }

        //使用SharedPreferences存储技术对城市CN码列表进行读取
        SharedPreferences c=getSharedPreferences("cities",MODE_PRIVATE);
        SharedPreferences p=getSharedPreferences("parentCities",MODE_PRIVATE);
        SharedPreferences hp=getSharedPreferences("historyPosition",MODE_PRIVATE);
        nowPosition=hp.getInt("Position",0);
        for(int i=0;i<5;i++){
            cityCNs[i]=c.getString("CN"+i,null);
            cityParents[i]=p.getString("CN"+i,null);
//            Log.i("Test", "存储的数据为："+cityCNs[i]);
            if(cityCNs[i]!=null){
                cityNum++;
//                Log.i("Test", "存储的数据为："+cityCNs[i]);
            }
        }
//        Log.i("Test", "存储的数据为："+c.getAll()+p.getAll());
    }

    //* 标题栏初始化
    private void transparentStatusBar() {
//        ActionBar actionbar = getSupportActionBar();
//        if (actionbar != null) {
//            actionbar.hide();
//        }
        if (Build.VERSION.SDK_INT >= 21) {
            View decorView = getWindow().getDecorView();
            int option = View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View
                    .SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN | View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View
                    .SYSTEM_UI_FLAG_IMMERSIVE_STICKY;
            decorView.setSystemUiVisibility(option);
            getWindow().setNavigationBarColor(Color.TRANSPARENT);
            getWindow().setStatusBarColor(Color.TRANSPARENT);
        }
        ActionBar actionBar = getSupportActionBar();
        actionBar.hide();
    }

    //*底部指示器初始化
    private LinearLayout indicatorLinearLayout;
    private void indicatorIni(){
        //创建底部指示器(小圆点)
        for (int pic=0; pic<cityNum;pic++) {
            View idicView = new View(WeatherShow.this);
            idicView.setBackgroundResource(R.drawable.indicator);
            indicatorLinearLayout=findViewById(R.id.indicator_linear);
            idicView.setEnabled(false);
            //设置宽高
            LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(20, 20);
            //设置间隔
//            if (pic != mPics[0]) {
            layoutParams.leftMargin = 5;
            layoutParams.rightMargin = 5;
//            }
            //添加到LinearLayout
            indicatorLinearLayout.addView(idicView, layoutParams);
        }
        indicatorLinearLayout.getChildAt(nowPosition).setEnabled(true);
        lastPosition=nowPosition;
    }

    //* 实现天气显示界面的初始化设置，利用fragment显示不同城市天气
    private void fragmentIni(){
        List<View> viewList=new ArrayList<View>();
        List<Fragment> fragmentList=new ArrayList<Fragment>();
        switch (cityNum){
            case 0:
            case 1:
                fragmentList.add(new Weather_Fragment1());

                break;
            case 2:
                fragmentList.add(new Weather_Fragment1());
                fragmentList.add(new Weather_Fragment2());
                break;
            case 3:
                fragmentList.add(new Weather_Fragment1());
                fragmentList.add(new Weather_Fragment2());
                fragmentList.add(new Weather_Fragment3());
                break;
            case 4:
                fragmentList.add(new Weather_Fragment1());
                fragmentList.add(new Weather_Fragment2());
                fragmentList.add(new Weather_Fragment3());
                fragmentList.add(new Weather_Fragment4());
                break;
            case 5:
                fragmentList.add(new Weather_Fragment1());
                fragmentList.add(new Weather_Fragment2());
                fragmentList.add(new Weather_Fragment3());
                fragmentList.add(new Weather_Fragment4());
                fragmentList.add(new Weather_Fragment5());
                break;
        }
//        List<String> titleList=new ArrayList<String>();
//        titleList.add("第一页");
//        titleList.add("第二页");
//        titleList.add("第三页");
//        titleList.add("第四页");
//        titleList.add("第五页");
//        //为页卡标题设置属性
//        PagerTabStrip tab=(PagerTabStrip)findViewById(R.id.weatherShow_PagerTabStrip);
//        tab.setBackgroundColor(Color.YELLOW);
//        tab.setTextColor(R.color.red);
//        tab.setDrawFullUnderline(false);
//        tab.setTabIndicatorColor(Color.BLUE);
        pagers=(ViewPager) findViewById(R.id.weatherShow_ViewPager);

        //创建PagerAdapter的适配器
//        MyPageAdapter adapter=new MyPageAdapter(viewList,null);
        FragmentPagerAdapter adapter2=new FragmentPagerAdapter(getSupportFragmentManager(),fragmentList,null);
//        //ViewPager加载适配器
        pagers.setAdapter(adapter2);
        pagers.setCurrentItem(nowPosition);
//        WeatherShow(nowPosition);

    }

    //*实现天气信息的手动下拉刷新,实现对当前界面城市的刷新
    private void refreshIni(){

        weather_flash=findViewById(R.id.weather_swipe);
        weather_flash.setColorSchemeResources(R.color.blue,R.color.green,R.color.red,R.color.green);
        weather_flash.setSize(SwipeRefreshLayout.LARGE);
        weather_flash.setProgressBackgroundColor(R.color.yellow);//设置下拉图标背景颜色
        weather_flash.setProgressViewEndTarget(true,100);//设置图标下拉限度

        weather_flash.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
//                Toast.makeText(WeatherShow.this, "正在刷新", Toast.LENGTH_SHORT).show();
                WeatherGet(cityCNs[nowPosition]);
                weather_flash.setRefreshing(false);//更新完成后关闭进度条
//                Toast.makeText(WeatherShow.this, "刷新成功", Toast.LENGTH_SHORT).show();
            }
        });
    }

     //* Timer初始化，通过Timer实现简单的天气信息的自动刷新
    private Timer timer;
    private TimerTask timertask;
    private int refreshTime=60;//自动更新的延时时间设置，单位为分钟
    private void timerIni(){
        timer = new Timer();
        timertask = new TimerTask() {
            @Override
            public void run() {                 //这个run 的使用就是开启了一个新的线程,将全部信息进行更新
                    WeatherGet(cityCNs[nowPosition]);
            }
        };
        timer.schedule(timertask,refreshTime*60*1000,refreshTime*60*1000);//delay是第一次刷新延时，period是刷新间隔，后期可以改成可调的

    }

    //* 通过NAME获得图片ID
    private Drawable getIDbyName(String name){
        ApplicationInfo appInfo =getApplicationInfo();
//                Context mContext=
        Resources resources = getResources();
        int resID = resources.getIdentifier(name, "drawable", appInfo.packageName);
//                Log.i("Test", "resID="+resID);
        Drawable drawable = resources.getDrawable(resID);
        return drawable;
    }

}

